package henu.graduation_design.onlineshop;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.json.JSONException;
import org.json.JSONObject;

import henu.graduation_design.adapter.ProductSearchAdapter;
import henu.graduation_design.tool.AnalyseProductSearchJson;
import henu.graduation_design.util.JsonLoader;
import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
import android.view.Window;
import android.widget.ListView;
import android.widget.Toast;

public class ProductSearchActivity extends Activity {

	private String keyWord;
	private JsonLoader jsonLoader = new JsonLoader();

	ListView searchListView;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		requestWindowFeature(Window.FEATURE_NO_TITLE);
		setContentView(R.layout.activity_product_search);
		
		Intent intent = getIntent();// 获得传过来的Intent。
		keyWord = intent.getStringExtra("KEY");// ��菁�ame取出值。
		searchListView = (ListView) findViewById(R.id.search_listview);

		try {
			Map<String,Object> jsonObject = new HashMap<String, Object>();
			jsonObject.put("keyword", keyWord);
			loadJson(getString(R.string.url_product_search), jsonObject);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	private void loadJson(final String url, final Map<String,Object> jsonObject) {
		jsonLoader.postJson(url,null, jsonObject,
				new JsonLoader.JsonObjectCallback() {
					public void jsonLoaded(JSONObject jsonObject) {
						List<HashMap<String, Object>> list = new ArrayList<HashMap<String, Object>>();
						AnalyseProductSearchJson analyse = new AnalyseProductSearchJson(
								jsonObject);
						try {
							list = analyse.getList();
						} catch (JSONException e) {
							Toast.makeText(getApplicationContext(),
									"解析category的json数据发生异常", Toast.LENGTH_LONG)
									.show();
						}

						searchListView.setAdapter(new ProductSearchAdapter(
								ProductSearchActivity.this, list,keyWord));
					}
				});
	}

}
